/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * subjects.java
 *
 * Created on 18.09.2011, 16:57:08
 */
package de.aeo.bll;

import java.awt.event.KeyEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
 * @author Frederic Branczyk & Robert Piwonski
 */
public class SubjectsForm extends javax.swing.JFrame {
    
    //Variable declaration Start
    
    private Algorithm a;
    private main mainFrame;
    private DefaultComboBoxModel subjectMinStudents;
    private DefaultComboBoxModel subjectMaxStudents;
    private DefaultComboBoxModel periods;
    private Subject editSubject;
    private int editSubjectIndex;
    
    //Variable declaration End

    /** Creates new form subjects */
    public SubjectsForm(String title, Algorithm a, main mainFrame) {
        this.setTitle(title);
        this.a = a;
        this.mainFrame = mainFrame;
        this.subjectMinStudents = new DefaultComboBoxModel();
        this.subjectMaxStudents = new DefaultComboBoxModel();
        for(int i=a.getSubjectMinMin(); i<=a.getSubjectMinMax(); i++) {
            this.subjectMinStudents.addElement(Integer.toString(i));
        }
        for(int i=a.getSubjectMaxMin(); i<=a.getSubjectMaxMax(); i++) {
            this.subjectMaxStudents.addElement(Integer.toString(i));
        }
        this.periods = new DefaultComboBoxModel();
        this.periods.addElement(this.a.getLanguageString("1year"));
        this.periods.addElement(this.a.getLanguageString("2years"));
        initComponents();
        this.jButtonApply.registerKeyboardAction(this.jButtonApply.getActionListeners()[0], KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_IN_FOCUSED_WINDOW);
        this.jButtonApply.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        this.jButtonApply.getActionMap();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldAbbreviation = new javax.swing.JTextField();
        jTextFieldName = new javax.swing.JTextField();
        jComboBoxMaxStudents = new javax.swing.JComboBox();
        jComboBoxMinStudents = new javax.swing.JComboBox();
        jButtonApply = new javax.swing.JButton();
        jComboBoxPeriod = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText(this.a.getLanguageString("subject")+":");

        jLabel2.setText(this.a.getLanguageString("abbreviation")+":");

        jLabel3.setText(this.a.getLanguageString("minStudents")+":");

        jLabel4.setText(this.a.getLanguageString("maxStudents")+":");

        jComboBoxMaxStudents.setModel(this.subjectMaxStudents);

        jComboBoxMinStudents.setModel(this.subjectMinStudents);

        jButtonApply.setText(this.a.getLanguageString("apply"));
        jButtonApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonApplyActionPerformed(evt);
            }
        });
        jButtonApply.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jButtonApplyKeyReleased(evt);
            }
        });

        jComboBoxPeriod.setModel(this.periods);

        jLabel5.setText(this.a.getLanguageString("period")+":");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonApply, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addComponent(jComboBoxMinStudents, javax.swing.GroupLayout.Alignment.TRAILING, 0, 163, Short.MAX_VALUE)
                    .addComponent(jComboBoxMaxStudents, javax.swing.GroupLayout.Alignment.TRAILING, 0, 163, Short.MAX_VALUE)
                    .addComponent(jComboBoxPeriod, 0, 163, Short.MAX_VALUE)
                    .addComponent(jTextFieldAbbreviation, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldAbbreviation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxMinStudents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxMaxStudents, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonApply)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
        try {
            this.apply();
        } catch (InterruptedException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.a.getLanguageString("titleWarning"), 0);
        }
    }//GEN-LAST:event_jButtonApplyActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            this.resetForm();
        } catch (InterruptedException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.a.getLanguageString("titleWarning"), 0);
        }
    }//GEN-LAST:event_formWindowClosing

    private void jButtonApplyKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonApplyKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonApplyKeyReleased

    public void setEditSubject(Subject editSubject) {
        this.editSubject = editSubject;
    }
    
    private Subject getEditSubject() {
        return this.editSubject;
    }

    public JComboBox getjComboBoxMaxStudents() {
        return jComboBoxMaxStudents;
    }

    public void setjComboBoxMaxStudents(JComboBox jComboBoxMaxStudents) {
        this.jComboBoxMaxStudents = jComboBoxMaxStudents;
    }

    public JComboBox getjComboBoxMinStudents() {
        return jComboBoxMinStudents;
    }

    public void setjComboBoxMinStudents(JComboBox jComboBoxMinStudents) {
        this.jComboBoxMinStudents = jComboBoxMinStudents;
    }

    public JTextField getjTextFieldAbbreviation() {
        return jTextFieldAbbreviation;
    }

    public void setjTextFieldAbbreviation(JTextField jTextFieldAbbreviation) {
        this.jTextFieldAbbreviation = jTextFieldAbbreviation;
    }

    public JTextField getjTextFieldName() {
        return jTextFieldName;
    }

    public void setjTextFieldName(JTextField jTextFieldName) {
        this.jTextFieldName = jTextFieldName;
    }

    public int getEditSubjectIndex() {
        return editSubjectIndex;
    }

    public void setEditSubjectIndex(int editSubjectIndex) {
        this.editSubjectIndex = editSubjectIndex;
    }

    public DefaultComboBoxModel getSubjectMaxStudents() {
        return subjectMaxStudents;
    }

    public void setSubjectMaxStudents(DefaultComboBoxModel subjectMaxStudents) {
        this.subjectMaxStudents = subjectMaxStudents;
    }

    public DefaultComboBoxModel getSubjectMinStudents() {
        return subjectMinStudents;
    }

    public void setSubjectMinStudents(DefaultComboBoxModel subjectMinStudents) {
        this.subjectMinStudents = subjectMinStudents;
    }

    public JComboBox getjComboBoxPeriod() {
        return jComboBoxPeriod;
    }

    public void setjComboBoxPeriod(JComboBox jComboBoxPeriod) {
        this.jComboBoxPeriod = jComboBoxPeriod;
    }
    
    public void resetForm() throws InterruptedException {
        this.setVisible(false);
        this.jTextFieldName.setText("");
        this.jTextFieldAbbreviation.setText("");
        this.jComboBoxMinStudents.setSelectedIndex(0);
        this.jComboBoxMaxStudents.setSelectedIndex(0);
        this.jComboBoxPeriod.setSelectedIndex(0);
        this.mainFrame.setSubjectEditable(true);
        this.mainFrame.setFocusable(true);
    }
    
    private void apply() throws InterruptedException {
        if(this.editSubject == null) {
            if(this.jTextFieldName.getText().isEmpty() || this.jTextFieldAbbreviation.getText().isEmpty()) {
                try {
                    throw new EmptySubjectException(this.a.getLanguageString("emptySubjectExceptionMsg"));
                } catch (EmptySubjectException ex) {
                    JOptionPane.showMessageDialog(this, ex.getMessage(), this.a.getLanguageString("titleWarning"), 0);
                }
            }else if(this.jTextFieldName.getText().contains(";") ||
                     this.jTextFieldName.getText().contains(";") ||
                     this.jTextFieldAbbreviation.getText().contains("2")) {
                JOptionPane.showMessageDialog(this, this.a.getLanguageString("illegalCharacterMsg")+" [\";\", \"2\"]", this.a.getLanguageString("titleWarning"), 0);
            }else {
                Subject s = new Subject(this.jTextFieldName.getText(), 
                        this.jTextFieldAbbreviation.getText(), 
                        this.jComboBoxMinStudents.getSelectedIndex()+this.a.getSubjectMinMin(), 
                        this.jComboBoxMaxStudents.getSelectedIndex()+this.a.getSubjectMaxMin(),
                        this.jComboBoxPeriod.getSelectedIndex()+1);
                this.a.addSubjectList(s);
                this.mainFrame.getJLabelSubjectCount().setText(Integer.toString(this.a.getSubjectCount()));
                DefaultListModel dlm = (DefaultListModel)this.mainFrame.getjListSubjects().getModel();
                dlm.insertElementAt(s, dlm.getSize()-1);
                resetForm();
            }
        }else {
            if(this.jTextFieldName.getText().contains(";") ||
               this.jTextFieldAbbreviation.getText().contains(";") ||
               this.jTextFieldAbbreviation.getText().contains("2")) {
                JOptionPane.showMessageDialog(this, this.a.getLanguageString("illegalCharacterMsg")+" [\";\", \"2\"]", this.a.getLanguageString("titleWarning"), 0);
            }else {
                this.editSubject.setName(this.jTextFieldName.getText());
                this.editSubject.setAbbreviation(this.jTextFieldAbbreviation.getText());
                this.editSubject.setMinStudents(this.jComboBoxMinStudents.getSelectedIndex()+this.a.getSubjectMinMin());
                this.editSubject.setMaxStudents(this.jComboBoxMaxStudents.getSelectedIndex()+this.a.getSubjectMaxMin());
                this.editSubject.setPeriod(this.jComboBoxPeriod.getSelectedIndex()+1);
                DefaultListModel dlm = (DefaultListModel)this.mainFrame.getjListSubjects().getModel();
                dlm.setElementAt(editSubject, this.editSubjectIndex);
                this.mainFrame.createPreview();
                resetForm();
            }
        }
    }
    
    public void reloadLanguageData() {
        this.jLabel1.setText(this.a.getLanguageString("subject")+":");
        this.jLabel2.setText(this.a.getLanguageString("abbreviation")+":");
        this.jLabel3.setText(this.a.getLanguageString("minStudents")+":");
        this.jLabel4.setText(this.a.getLanguageString("maxStudents")+":");
        this.jLabel5.setText(this.a.getLanguageString("period")+":");
        this.setTitle(this.a.getLanguageString("subject"));
        this.periods = new DefaultComboBoxModel();
        this.periods.addElement(this.a.getLanguageString("1year"));
        this.periods.addElement(this.a.getLanguageString("2years"));
        this.jComboBoxPeriod.setModel(periods);
        this.jButtonApply.setText(this.a.getLanguageString("apply"));
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonApply;
    private javax.swing.JComboBox jComboBoxMaxStudents;
    private javax.swing.JComboBox jComboBoxMinStudents;
    private javax.swing.JComboBox jComboBoxPeriod;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextFieldAbbreviation;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
}
